home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / os-prober / common.sh
Text File  |  2009-09-21  |  6KB  |  249 lines

  1. newns () {
  2.   [ "$OS_PROBER_NEWNS" ] || exec /usr/lib/os-prober/newns "$0" "$@"
  3. }
  4.  
  5. require_tmpdir() {
  6.   if [ -z "$OS_PROBER_TMP" ]; then
  7.     if type mktemp >/dev/null 2>&1; then
  8.       export OS_PROBER_TMP="$(mktemp -d /tmp/os-prober.XXXXXX)"
  9.       trap "rm -rf $OS_PROBER_TMP" EXIT HUP INT QUIT TERM
  10.     else
  11.       export OS_PROBER_TMP=/tmp
  12.     fi
  13.   fi
  14. }
  15.  
  16. count_for() {
  17.   _labelprefix=$1
  18.   _result=$(grep "^${_labelprefix} " /var/lib/os-prober/labels 2>/dev/null || true)
  19.  
  20.   if [ -z "$_result" ]; then
  21.     return
  22.   else
  23.     echo "$_result" | cut -d' ' -f2
  24.   fi
  25. }
  26.  
  27. count_next_label() {
  28.   require_tmpdir
  29.  
  30.   _labelprefix=$1
  31.   _cfor="$(count_for ${_labelprefix})"
  32.  
  33.   if [ -z "$_cfor" ]; then
  34.     echo "${_labelprefix} 1" >> /var/lib/os-prober/labels
  35.   else
  36.     sed "s/^${_labelprefix} ${_cfor}/${_labelprefix} $(($_cfor + 1))/" /var/lib/os-prober/labels > "$OS_PROBER_TMP/os-prober.tmp"
  37.     mv "$OS_PROBER_TMP/os-prober.tmp" /var/lib/os-prober/labels
  38.   fi
  39.   
  40.   echo "${_labelprefix}${_cfor}"
  41. }
  42.  
  43. progname=
  44. cache_progname() {
  45.   case $progname in
  46.     '')
  47.       progname="$(basename $0)"
  48.       ;;
  49.   esac
  50. }
  51.  
  52. log() {
  53.   cache_progname
  54.   logger -t "$progname" "$@"
  55. }
  56.  
  57. error() {
  58.   log "error: $@"
  59. }
  60.  
  61. warn() {
  62.   log "warning: $@"
  63. }
  64.  
  65. debug() {
  66.   log "debug: $@"
  67. }
  68.  
  69. result () {
  70.   log "result:" "$@"
  71.   echo "$@"
  72. }
  73.  
  74. # shim to make it easier to use os-prober outside d-i
  75. if ! type mapdevfs >/dev/null 2>&1; then
  76.   mapdevfs () {
  77.     readlink -f "$1"
  78.   }
  79. fi
  80.  
  81. item_in_dir () {
  82.     if [ "$1" = "-q" ]; then
  83.         q="-q"
  84.         shift 1
  85.     else
  86.         q=""
  87.     fi
  88.     # find files with any case
  89.     ls -1 "$2" | grep $q -i "^$1$"
  90. }
  91.  
  92. # We can't always tell the filesystem type up front, but if we have the
  93. # information then we should use it. Note that we can't use block-attr here
  94. # as it's only available in udebs.
  95. fs_type () {
  96.     if (export PATH="/lib/udev:$PATH"; type vol_id) >/dev/null 2>&1; then
  97.         PATH="/lib/udev:$PATH" vol_id --type "$1" 2>/dev/null
  98.     elif type blkid >/dev/null 2>&1; then
  99.         blkid -o value -s TYPE "$1" 2>/dev/null
  100.     else
  101.         return 0
  102.     fi
  103. }
  104.  
  105. parse_proc_mounts () {
  106.     while read -r line; do
  107.         set -- $line
  108.         printf '%s %s %s\n' "$(mapdevfs $1)" "$2" "$3"
  109.     done
  110. }
  111.  
  112. parsefstab () {
  113.     while read -r line; do
  114.         case "$line" in
  115.             "#"*)
  116.                 :    
  117.             ;;
  118.             *)
  119.                 set -- $line
  120.                 printf '%s %s %s\n' "$1" "$2" "$3"
  121.             ;;
  122.         esac
  123.     done
  124. }
  125.  
  126. unescape_mount () {
  127.     printf %s "$1" | \
  128.         sed 's/\\011/    /g; s/\\012/\n/g; s/\\040/ /g; s/\\134/\\/g'
  129. }
  130.  
  131. linux_mount_boot () {
  132.     partition="$1"
  133.     tmpmnt="$2"
  134.  
  135.     bootpart=""
  136.     mounted=""
  137.     if [ -e "$tmpmnt/etc/fstab" ]; then
  138.         # Try to mount any /boot partition.
  139.         bootmnt=$(parsefstab < "$tmpmnt/etc/fstab" | grep " /boot ") || true
  140.         if [ -n "$bootmnt" ]; then
  141.             set -- $bootmnt
  142.             boottomnt=""
  143.  
  144.             # Try to map labels and UUIDs ourselves if possible,
  145.             # so that we can check whether they're already
  146.             # mounted somewhere else.
  147.             if echo "$1" | grep -q "LABEL="; then
  148.                 label="$(echo "$1" | cut -d = -f 2)"
  149.                 if [ -h "/dev/disk/by-label/$label" ]; then
  150.                     shift
  151.                     set -- "$(readlink -f "/dev/disk/by-label/$label")" "$@"
  152.                     debug "mapped LABEL=$label to $1"
  153.                 fi
  154.             fi
  155.             if echo "$1" | grep -q "UUID="; then
  156.                 uuid="$(echo "$1" | cut -d = -f 2)"
  157.                 if [ -h "/dev/disk/by-uuid/$uuid" ]; then
  158.                     shift
  159.                     set -- "$(readlink -f "/dev/disk/by-uuid/$uuid")" "$@"
  160.                     debug "mapped UUID=$uuid to $1"
  161.                 fi
  162.             fi
  163.             tmppart="$1"
  164.             shift
  165.             set -- "$(mapdevfs "$tmppart")" "$@"
  166.  
  167.             # This is an awful hack and isn't guaranteed to
  168.             # work, but is the best we can do until busybox
  169.             # mount supports -L/-U.
  170.             smart_ldlp=
  171.             smart_mount=mount
  172.             if mount --help 2>&1 | head -n1 | grep -iq busybox; then
  173.                 if [ -x "$tmpmnt/bin/mount" ]; then
  174.                     smart_ldlp="$tmpmnt/lib"
  175.                     smart_mount="$tmpmnt/bin/mount"
  176.                 elif [ -x /target/bin/mount ]; then
  177.                     smart_ldlp=/target/lib
  178.                     smart_mount=/target/bin/mount
  179.                 fi
  180.             fi
  181.             if grep -q "^$1 " "$OS_PROBER_TMP/mounted-map"; then
  182.                 bindfrom="$(grep "^$1 " "$OS_PROBER_TMP/mounted-map" | head -n1 | cut -d " " -f 2)"
  183.                 bindfrom="$(unescape_mount "$bindfrom")"
  184.                 if [ "$bindfrom" != "$tmpmnt/boot" ]; then
  185.                     if mount --bind "$bindfrom" "$tmpmnt/boot"; then
  186.                         mounted=1
  187.                         bootpart="$1"
  188.                     else
  189.                         debug "failed to bind-mount $bindfrom onto $tmpmnt/boot"
  190.                     fi
  191.                 fi
  192.             fi
  193.             if [ "$mounted" ]; then
  194.                 :
  195.             elif [ -e "$1" ]; then
  196.                 bootpart="$1"
  197.                 boottomnt="$1"
  198.             elif [ -e "$tmpmnt/$1" ]; then
  199.                 bootpart="$1"
  200.                 boottomnt="$tmpmnt/$1"
  201.             elif [ -e "/target/$1" ]; then
  202.                 bootpart="$1"
  203.                 boottomnt="/target/$1"
  204.             elif echo "$1" | grep -q "LABEL="; then
  205.                 debug "mounting boot partition by label for linux system on $partition: $1"
  206.                 label=$(echo "$1" | cut -d = -f 2)
  207.                 if LD_LIBRARY_PATH=$smart_ldlp $smart_mount -L "$label" -o ro "$tmpmnt/boot" -t "$3"; then
  208.                     mounted=1
  209.                     bootpart=$(mount | grep "$tmpmnt/boot" | cut -d " " -f 1)
  210.                 else
  211.                     error "failed to mount by label"
  212.                 fi
  213.             elif echo "$1" | grep -q "UUID="; then
  214.                 debug "mounting boot partition by UUID for linux system on $partition: $1"
  215.                 uuid=$(echo "$1" | cut -d = -f 2)
  216.                 if LD_LIBRARY_PATH=$smart_ldlp $smart_mount -U "$uuid" -o ro "$tmpmnt/boot" -t "$3"; then
  217.                     mounted=1
  218.                     bootpart=$(mount | grep "$tmpmnt/boot" | cut -d " " -f 1)
  219.                 else
  220.                     error "failed to mount by UUID"
  221.                 fi
  222.             else
  223.                 bootpart=""
  224.             fi
  225.  
  226.             if [ ! "$mounted" ]; then
  227.                 if [ -z "$bootpart" ]; then
  228.                     debug "found boot partition $1 for linux system on $partition, but cannot map to existing device"
  229.                 else
  230.                     debug "found boot partition $bootpart for linux system on $partition"
  231.                     if mount -o ro "$boottomnt" "$tmpmnt/boot" -t "$3"; then
  232.                         mounted=1
  233.                     else
  234.                         error "failed to mount $boottomnt on $tmpmnt/boot"
  235.                     fi
  236.                 fi
  237.             fi
  238.         fi
  239.     fi
  240.     if [ -z "$bootpart" ]; then
  241.         bootpart="$partition"
  242.     fi
  243.     if [ -z "$mounted" ]; then
  244.         mounted=0
  245.     fi
  246.  
  247.     echo "$bootpart $mounted"
  248. }
  249.